
package com.hlkj.pay.task;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import cn.hutool.core.date.DatePattern;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.dto.merchant.MerchantAppQueryDto;
import com.hlkj.pay.dto.merchant.MerchantInfoQueryDto;
import com.hlkj.pay.dto.merchant.access.MerchantAccessAuthQueryDto;
import com.hlkj.pay.dto.order.ComplaintOrderQueryDto;
import com.hlkj.pay.dto.order.OrderStatisticsQueryDto;
import com.hlkj.pay.dto.order.PayOrderQueryDto;
import com.hlkj.pay.dto.order.RefundOrderQueryDto;
import com.hlkj.pay.dto.pay.PayChannelQueryDto;
import com.hlkj.pay.dto.pay.PayWayQueryDto;
import com.hlkj.pay.dto.payment.MerchantPaymentDetailDto;
import com.hlkj.pay.dto.payment.MerchantPaymentPageQueryDto;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.enums.PayEnums;
import com.hlkj.pay.enums.PayOrderEnums;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAccessAuthDO;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAppChannelDO;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAppDO;
import com.hlkj.pay.infrastructure.model.merchant.MerchantInfoDO;
import com.hlkj.pay.infrastructure.model.order.OrderStatisticsDO;
import com.hlkj.pay.infrastructure.model.pay.PayChannelDO;
import com.hlkj.pay.infrastructure.model.pay.PayWayDO;
import com.hlkj.pay.service.merchant.IMerchantAccessAuthService;
import com.hlkj.pay.service.merchant.IMerchantApplicationService;
import com.hlkj.pay.service.merchant.IMerchantService;
import com.hlkj.pay.service.order.IOrderComplaintService;
import com.hlkj.pay.service.order.IOrderStatisticsService;
import com.hlkj.pay.service.order.IPayOrderService;
import com.hlkj.pay.service.order.IRefundOrderService;
import com.hlkj.pay.service.pay.IPayChannelService;
import com.hlkj.pay.service.pay.IPayWayService;
import com.hlkj.pay.service.payment.IMerchantPaymentService;
import com.hlkj.pay.util.DateUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author HlpayTeam
 * @date 2024/10/29 10:43
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class PayOrderStatisticsTasks {

    private final IPayOrderService payOrderService;

    private final IRefundOrderService refundOrderService;

    private final IMerchantService merchantService;

    private final IPayWayService payWayService;

    private final IMerchantApplicationService merchantApplicationService;

    private final IOrderStatisticsService statisticsService;

    private final IOrderComplaintService orderComplaintService;

    private final IPayChannelService payChannelService;

    private final IMerchantPaymentService paymentService;

    private final IMerchantAccessAuthService merchantAccessAuthService;

    @Value("${server.port:8020}")
    private String serverPort;

    @Value("${hlkj.task.port:8020}")
    private String taskPort;

    // 2点进行统计
    @Scheduled(cron = "0 0 2 * * *")
    public void cronTask() {
        if (!serverPort.equals(taskPort)) {
            log.info("当前task port 非目标port serverPort:{},taskPort:{}", serverPort, taskPort);
            return;
        }
        processOrderStatistics(null);
    }

    public void processOrderStatistics(String startDate, String endDate) {
        if (!StringUtils.hasText(startDate)) {
            return;
        }
        if (!StringUtils.hasText(endDate)) {
            processOrderStatistics(startDate);
            return;
        }
        LocalDate startLocalDate = LocalDate.parse(startDate);
        LocalDate endLocalDate = LocalDate.parse(endDate);
        // 计算天数差
        long daysBetween = ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
        for (int i = 0; i < daysBetween; i++) {
            processOrderStatistics(startLocalDate.plusDays(i).toString());
        }
    }

    public void processOrderStatistics(String date) {
        if (!StringUtils.hasText(date)) {
            date = DateUtils.offsetDay(DateUtils.getDateString(DatePattern.NORM_DATE_PATTERN), DatePattern.NORM_DATE_PATTERN, -1);
        }
        log.info("处理订单统计的日期为:{}",date);
        // 统计昨天的数据
        LocalDate localDate = LocalDate.parse(date);

        long startQueryTime = LocalDateTime.of(localDate.getYear(), localDate.getMonth(), localDate.getDayOfMonth(), 0, 0, 0).atZone(ZoneId.of("Asia/Shanghai")).toInstant()
                .toEpochMilli();
        long endQueryTime = LocalDateTime.of(localDate.getYear(), localDate.getMonth(), localDate.getDayOfMonth(), 23, 59, 59).atZone(ZoneId.of("Asia/Shanghai")).toInstant()
                .toEpochMilli();

        log.info("处理订单统计时间范围:开始时间#{} 结束时间#{}",startQueryTime, endQueryTime);
        MerchantInfoQueryDto merchantInfoQueryDto = new MerchantInfoQueryDto();
        merchantInfoQueryDto.setSnType(MerchantEnum.MERCHANT_SN_TYPE.MERCHANT.getCode());
        List<MerchantInfoDO> merchantInfoDOList = merchantService.queryList(merchantInfoQueryDto);
        // 商户维度
        List<OrderStatisticsDO> addStatisticsList = new ArrayList<>();
        for (MerchantInfoDO merchantInfoDO : merchantInfoDOList) {
            // 所有的统计都是基于订单提交订单和支付的消息 先统计该商户下当天的提交订单数量和支付的数量 还有退款的消息数量是否正确
            // 如果正确 说明通过消息通知 统计的结果正确,无需通过表数据再次统计
            OrderStatisticsQueryDto orderStatisticsQueryDto = new OrderStatisticsQueryDto();
            orderStatisticsQueryDto.setType(PayOrderEnums.ORDER_STATISTICS_TYPE.SN.getCode());
            orderStatisticsQueryDto.setTime(startQueryTime);
            orderStatisticsQueryDto.setSn(merchantInfoDO.getSn());
            List<OrderStatisticsDO> statisticsList = statisticsService.queryStatistics(orderStatisticsQueryDto);

            PayOrderQueryDto payOrderQueryDto = new PayOrderQueryDto();
            payOrderQueryDto.setStartQueryTime(startQueryTime);
            payOrderQueryDto.setEndQueryTime(endQueryTime);
            payOrderQueryDto.setSn(merchantInfoDO.getSn());
            Integer submitOrderCount = payOrderService.orderCount(payOrderQueryDto);
            payOrderQueryDto.setState(PayOrderEnums.PAY_ORDER_STATUS.SUCCESS.getCode());
            Integer payOrderCount = payOrderService.orderCount(payOrderQueryDto);
            RefundOrderQueryDto refundOrderQueryDto = new RefundOrderQueryDto();
            refundOrderQueryDto.setStartQueryTime(startQueryTime);
            refundOrderQueryDto.setEndQueryTime(endQueryTime);
            refundOrderQueryDto.setSn(merchantInfoDO.getSn());

            //统计代付
            MerchantPaymentPageQueryDto paymentPageQueryDto = new MerchantPaymentPageQueryDto();
            paymentPageQueryDto.setStartQueryTime(startQueryTime);
            paymentPageQueryDto.setEndQueryTime(endQueryTime);
            paymentPageQueryDto.setSn(merchantInfoDO.getSn());
            List<MerchantPaymentDetailDto> paymentDetailDtoList =  paymentService.queryList(paymentPageQueryDto);

            // 只统计退款完成
            refundOrderQueryDto.setRefundStatus(PayOrderEnums.REFUND_STATUS.REFUND.getCode());
            Integer refundOrderCount = refundOrderService.orderCount(refundOrderQueryDto);
            if ((submitOrderCount == null || submitOrderCount == 0) && (refundOrderCount == null || refundOrderCount == 0) && paymentDetailDtoList.isEmpty()) {
                log.info("订单统计重算job date :{},实际的提交订单和退款订单都是0 :{}",date, merchantInfoDO.getSn());
                continue;
            }

            ComplaintOrderQueryDto complaintOrderQueryDto = new ComplaintOrderQueryDto();
            complaintOrderQueryDto.setStartQueryTime(startQueryTime);
            complaintOrderQueryDto.setEndQueryTime(endQueryTime);
            complaintOrderQueryDto.setSn(merchantInfoDO.getSn());
            Integer complaintCount = orderComplaintService.countBySn(complaintOrderQueryDto);

            Integer wxOrderCount = getWxCount(startQueryTime, endQueryTime, merchantInfoDO.getSn());

            boolean check = true;
            String remark = null;
            if (!CollectionUtils.isEmpty(statisticsList)) {
                OrderStatisticsDO orderStatisticsDO = statisticsList.get(0);
                if (!orderStatisticsDO.getSubmitOrderCount().equals(submitOrderCount) || !orderStatisticsDO.getRefundCount().equals(refundOrderCount)
                        || !orderStatisticsDO.getPayOrderCount().equals(payOrderCount) || !complaintCount.equals(orderStatisticsDO.getComplainCount()) || !wxOrderCount.equals(orderStatisticsDO.getWxOrderCount())) {
                    log.info("原统计数据与实际查询订单、退款数据不匹配 统计提交订单数量:submitOrderCount:{},统计支付订单数量:submitOrderCount:{},统计退款订单数量:submitOrderCount:{},统计投诉订单数量:{},统计微信订单数量:{}", orderStatisticsDO.getSubmitOrderCount(),
                            orderStatisticsDO.getPayOrderCount(), orderStatisticsDO.getRefundCount(), orderStatisticsDO.getComplainCount(), orderStatisticsDO.getWxOrderCount());
                    log.info("原统计数据与实际查询订单、退款数据不匹配 实际提交订单数量:submitOrderCount:{},实际支付订单数量:submitOrderCount:{},实际退款订单数量:submitOrderCount:{},实际投诉订单数量:complaintCount:{},实际微信订单数量:{}", submitOrderCount, payOrderCount,
                            refundOrderCount, complaintCount, wxOrderCount);
                    remark = "统计错误 需要重算 统计提交订单数量:" + orderStatisticsDO.getSubmitOrderCount() + ",统计支付订单数量:" + orderStatisticsDO.getPayOrderCount() + ",统计退款订单数量:"
                            + orderStatisticsDO.getRefundCount() + ",统计投诉订单数量:" + orderStatisticsDO.getComplainCount() + ",统计微信订单数量:" + orderStatisticsDO.getWxOrderCount();
                    remark = remark + ";实际订单数量:" + submitOrderCount + ",实际支付订单数量:" + payOrderCount + ",实际退款订单数量:" + refundOrderCount + ",实际投诉订单数量:" + complaintCount + ",实际微信订单数量:" + wxOrderCount;
                    check = false;
                }
                else {
                    log.info("原统计数据与实际查询订单、退款数据匹配 统计提交订单数量:submitOrderCount:{},统计支付订单数量:submitOrderCount:{},统计退款订单数量:submitOrderCount:{},统计投诉订单数量:complaintCount:{}", orderStatisticsDO.getSubmitOrderCount(),
                            orderStatisticsDO.getPayOrderCount(), orderStatisticsDO.getRefundCount(), orderStatisticsDO.getComplainCount());
                    log.info("原统计数据与实际查询订单、退款数据匹配 实际提交订单数量:submitOrderCount:{},实际支付订单数量:submitOrderCount:{},实际退款订单数量:submitOrderCount:{},实际投诉订单数量:complaintCount:{}", submitOrderCount, payOrderCount,
                            refundOrderCount, complaintCount);
                }
            }
            if (check && !CollectionUtils.isEmpty(statisticsList)) {
                continue;
            }
            if (!CollectionUtils.isEmpty(statisticsList)) {
                statisticsService.deleteByTime(startQueryTime, merchantInfoDO.getSn(), remark);
            }

            // sn 维度
            OrderStatisticsDO orderSnStatisticsDO = querySnStatistics(merchantInfoDO.getSn(), startQueryTime, endQueryTime);
            if ((orderSnStatisticsDO.getSubmitOrderCount() != null && orderSnStatisticsDO.getSubmitOrderCount().compareTo(0) != 0)
                    || (orderSnStatisticsDO.getRefundCount() != null && orderSnStatisticsDO.getRefundCount().compareTo(0) != 0) || orderSnStatisticsDO.getPaymentCount().compareTo(0) != 0) {
                orderSnStatisticsDO.setTime(startQueryTime);
                orderSnStatisticsDO.setSn(merchantInfoDO.getSn());
                orderSnStatisticsDO.setComplainCount(complaintCount);
                orderSnStatisticsDO.setWxOrderCount(wxOrderCount);
                orderSnStatisticsDO.setRelationId(merchantInfoDO.getSn());
                orderSnStatisticsDO.setType(PayOrderEnums.ORDER_STATISTICS_TYPE.SN.getCode());
                addStatisticsList.add(orderSnStatisticsDO);
            }

            // 支付方式维度
            PayWayQueryDto payWayQueryDto = new PayWayQueryDto();
            payWayQueryDto.setStatus(CommonEnum.YES_NO_TYPE.YES.getCode());
            List<PayWayDO> payWayDOS = payWayService.queryList(payWayQueryDto);
            Map<Integer, List<PayWayDO>> collect = payWayDOS.stream().collect(Collectors.groupingBy(payWayDO -> payWayDO.getType()));
            collect.forEach((key, value) -> {
                List<String> payWayCodes = value.stream().map(payWayDO -> payWayDO.getCode()).collect(Collectors.toList());
                OrderStatisticsDO orderPayStatisticsDO = queryPayTypeStatistics(merchantInfoDO.getSn(), payWayCodes, startQueryTime, endQueryTime);
                orderPayStatisticsDO.setTime(startQueryTime);
                orderPayStatisticsDO.setSn(merchantInfoDO.getSn());
                orderPayStatisticsDO.setRelationId(key.toString());
                orderPayStatisticsDO.setComplainCount(complaintCount);
                orderPayStatisticsDO.setWxOrderCount(wxOrderCount);
                orderPayStatisticsDO.setType(PayOrderEnums.ORDER_STATISTICS_TYPE.PAY_TYPE.getCode());
                if ((orderPayStatisticsDO.getSubmitOrderCount() != null && orderPayStatisticsDO.getSubmitOrderCount().compareTo(0) != 0)
                        || (orderPayStatisticsDO.getRefundCount() != null && orderPayStatisticsDO.getRefundCount().compareTo(0) != 0)) {
                    addStatisticsList.add(orderPayStatisticsDO);
                }
            });
            // app维度
            MerchantAppQueryDto merchantAppQueryDto = new MerchantAppQueryDto();
            merchantAppQueryDto.setStatus(CommonEnum.YES_NO_TYPE.YES.getCode());
            merchantAppQueryDto.setSn(merchantInfoDO.getSn());
            List<MerchantAppDO> merchantAppList = merchantApplicationService.queryList(merchantAppQueryDto);
            merchantAppList.stream().forEach(merchantAppDO -> {
                OrderStatisticsDO orderAppStatisticsDO = queryAppIdStatistics(merchantInfoDO.getSn(), merchantAppDO.getAppId(), startQueryTime, endQueryTime);
                orderAppStatisticsDO.setTime(startQueryTime);
                orderAppStatisticsDO.setSn(merchantInfoDO.getSn());
                orderAppStatisticsDO.setRelationId(merchantAppDO.getAppId());
                orderAppStatisticsDO.setComplainCount(complaintCount);
                orderAppStatisticsDO.setWxOrderCount(wxOrderCount);
                orderAppStatisticsDO.setType(PayOrderEnums.ORDER_STATISTICS_TYPE.APPID.getCode());
                if ((orderAppStatisticsDO.getSubmitOrderCount() != null && orderAppStatisticsDO.getSubmitOrderCount().compareTo(0) != 0)
                        || (orderAppStatisticsDO.getRefundCount() != null && orderAppStatisticsDO.getRefundCount().compareTo(0) != 0)) {
                    addStatisticsList.add(orderAppStatisticsDO);
                }

            });
            // mchChannelCode维度
            List<MerchantAppChannelDO> merchantAppChannelDOS = merchantApplicationService.queryChannelInfoList(merchantAppQueryDto);
            merchantAppChannelDOS.stream().forEach(merchantAppChannelDO -> {
                OrderStatisticsDO orderAppChannelStatisticsDO = queryMchChannelCodeStatistics(merchantInfoDO.getSn(), merchantAppChannelDO.getCode(), startQueryTime, endQueryTime);
                orderAppChannelStatisticsDO.setTime(startQueryTime);
                orderAppChannelStatisticsDO.setSn(merchantInfoDO.getSn());
                orderAppChannelStatisticsDO.setComplainCount(complaintCount);
                orderAppChannelStatisticsDO.setWxOrderCount(wxOrderCount);
                orderAppChannelStatisticsDO.setRelationId(merchantAppChannelDO.getCode());
                orderAppChannelStatisticsDO.setType(PayOrderEnums.ORDER_STATISTICS_TYPE.MCH_CHANNEL_CODE.getCode());
                if ((orderAppChannelStatisticsDO.getSubmitOrderCount() != null && orderAppChannelStatisticsDO.getSubmitOrderCount().compareTo(0) != 0)
                        || (orderAppChannelStatisticsDO.getRefundCount() != null && orderAppChannelStatisticsDO.getRefundCount().compareTo(0) != 0)) {
                    addStatisticsList.add(orderAppChannelStatisticsDO);
                }
            });

            // channel code 支付渠道统计
            PayChannelQueryDto payWayQueryDO = new PayChannelQueryDto();
            List<PayChannelDO> payChannelDOList = payChannelService.queryList(payWayQueryDO);
            payChannelDOList.stream().forEach(payChannel -> {
                OrderStatisticsDO orderChannelStatisticsDO = queryPayChannelCodeStatistics(merchantInfoDO.getSn(), payChannel.getCode(), startQueryTime, endQueryTime);
                orderChannelStatisticsDO.setTime(startQueryTime);
                orderChannelStatisticsDO.setSn(merchantInfoDO.getSn());
                orderChannelStatisticsDO.setComplainCount(complaintCount);
                orderChannelStatisticsDO.setWxOrderCount(wxOrderCount);
                orderChannelStatisticsDO.setRelationId(payChannel.getCode());
                orderChannelStatisticsDO.setType(PayOrderEnums.ORDER_STATISTICS_TYPE.CHANNEL_CODE.getCode());
                if ((orderChannelStatisticsDO.getSubmitOrderCount() != null && orderChannelStatisticsDO.getSubmitOrderCount().compareTo(0) != 0)
                        || (orderChannelStatisticsDO.getRefundCount() != null && orderChannelStatisticsDO.getRefundCount().compareTo(0) != 0)) {
                    addStatisticsList.add(orderChannelStatisticsDO);
                }
            });

            // aut 支付子通道 没有aut的情况不统计
            MerchantAccessAuthQueryDto merchantAccessAuthQueryDto = new MerchantAccessAuthQueryDto();
            List<MerchantAccessAuthDO> authDOList = merchantAccessAuthService.queryAuthList(merchantAccessAuthQueryDto);
            authDOList.stream().forEach(authDo -> {
                OrderStatisticsDO orderChannelStatisticsDO = queryAutCodeStatistics(merchantInfoDO.getSn(), authDo.getCode(), startQueryTime, endQueryTime);
                if (orderChannelStatisticsDO.getSubmitOrderCount() > 0) {
                    orderChannelStatisticsDO.setTime(startQueryTime);
                    orderChannelStatisticsDO.setSn(merchantInfoDO.getSn());
                    orderChannelStatisticsDO.setComplainCount(complaintCount);
                    orderChannelStatisticsDO.setWxOrderCount(wxOrderCount);
                    orderChannelStatisticsDO.setRelationId(authDo.getCode());
                    orderChannelStatisticsDO.setType(PayOrderEnums.ORDER_STATISTICS_TYPE.AUT_CODE.getCode());
                    if ((orderChannelStatisticsDO.getSubmitOrderCount() != null && orderChannelStatisticsDO.getSubmitOrderCount().compareTo(0) != 0)
                            || (orderChannelStatisticsDO.getRefundCount() != null && orderChannelStatisticsDO.getRefundCount().compareTo(0) != 0)) {
                        addStatisticsList.add(orderChannelStatisticsDO);
                    }
                }
            });
        }
        if (CollectionUtils.isNotEmpty(addStatisticsList)) {
            statisticsService.addStatisticsList(addStatisticsList);
        }
    }

    Integer getWxCount(long startQueryTime, long endQueryTime, String sn) {
        PayOrderQueryDto wxQueryCount = new PayOrderQueryDto();
        wxQueryCount.setStartQueryTime(startQueryTime);
        wxQueryCount.setEndQueryTime(endQueryTime);
        wxQueryCount.setSn(sn);
        wxQueryCount.setState(PayOrderEnums.PAY_ORDER_STATUS.SUCCESS.getCode());
        wxQueryCount.setPayType(PayEnums.PAY_TYPE.WECHAT.getType());
        return payOrderService.orderCount(wxQueryCount);
    }

    // 关联id 与type 匹配 1 为sn 2 为pay_type 3 app_id 4 mch_channel_code
    /**
     * 商户维度
     * 
     * @param sn
     * @param startQueryTime
     * @param endQueryTime
     * @return
     */
    OrderStatisticsDO querySnStatistics(String sn, Long startQueryTime, Long endQueryTime) {
        OrderStatisticsQueryDto orderStatisticsQueryDto = new OrderStatisticsQueryDto();
        orderStatisticsQueryDto.setStartQueryTime(startQueryTime);
        orderStatisticsQueryDto.setEndQueryTime(endQueryTime);
        orderStatisticsQueryDto.setSn(sn);
        return statisticsService.queryOrderStatistics(orderStatisticsQueryDto);
    }

    /**
     * 支付方式维度
     * 
     * @param payWayCodes
     * @param startQueryTime
     * @param endQueryTime
     * @return
     */
    OrderStatisticsDO queryPayTypeStatistics(String sn, List<String> payWayCodes, Long startQueryTime, Long endQueryTime) {
        OrderStatisticsQueryDto orderStatisticsQueryDto = new OrderStatisticsQueryDto();
        orderStatisticsQueryDto.setStartQueryTime(startQueryTime);
        orderStatisticsQueryDto.setEndQueryTime(endQueryTime);
        orderStatisticsQueryDto.setPayWayCodes(payWayCodes);
        orderStatisticsQueryDto.setSn(sn);
        return statisticsService.queryOrderStatistics(orderStatisticsQueryDto);

    }

    /**
     * appId 维度
     * 
     * @param appId
     * @param startQueryTime
     * @param endQueryTime
     * @return
     */
    OrderStatisticsDO queryAppIdStatistics(String sn, String appId, Long startQueryTime, Long endQueryTime) {
        OrderStatisticsQueryDto orderStatisticsQueryDto = new OrderStatisticsQueryDto();
        orderStatisticsQueryDto.setStartQueryTime(startQueryTime);
        orderStatisticsQueryDto.setEndQueryTime(endQueryTime);
        orderStatisticsQueryDto.setAppId(appId);
        orderStatisticsQueryDto.setSn(sn);
        return statisticsService.queryOrderStatistics(orderStatisticsQueryDto);
    }

    /**
     * mchChannelCode 维度统计
     * 
     * @param mchChannelCode
     * @param startQueryTime
     * @param endQueryTime
     * @return
     */
    OrderStatisticsDO queryMchChannelCodeStatistics(String sn, String mchChannelCode, Long startQueryTime, Long endQueryTime) {
        OrderStatisticsQueryDto orderStatisticsQueryDto = new OrderStatisticsQueryDto();
        orderStatisticsQueryDto.setStartQueryTime(startQueryTime);
        orderStatisticsQueryDto.setEndQueryTime(endQueryTime);
        orderStatisticsQueryDto.setMchChannelCode(mchChannelCode);
        orderStatisticsQueryDto.setSn(sn);
        return statisticsService.queryOrderStatistics(orderStatisticsQueryDto);
    }

    /**
     * PayChannel 支付渠道
     *
     * @param channelCode
     * @param startQueryTime
     * @param endQueryTime
     * @return
     */
    OrderStatisticsDO queryPayChannelCodeStatistics(String sn, String channelCode, Long startQueryTime, Long endQueryTime) {
        OrderStatisticsQueryDto orderStatisticsQueryDto = new OrderStatisticsQueryDto();
        orderStatisticsQueryDto.setStartQueryTime(startQueryTime);
        orderStatisticsQueryDto.setEndQueryTime(endQueryTime);
        orderStatisticsQueryDto.setChannelCode(channelCode);
        orderStatisticsQueryDto.setSn(sn);
        return statisticsService.queryOrderStatistics(orderStatisticsQueryDto);
    }

    /**
     * AutCode
     *
     * @param autCode
     * @param startQueryTime
     * @param endQueryTime
     * @return
     */
    OrderStatisticsDO queryAutCodeStatistics(String sn, String autCode, Long startQueryTime, Long endQueryTime) {
        OrderStatisticsQueryDto orderStatisticsQueryDto = new OrderStatisticsQueryDto();
        orderStatisticsQueryDto.setStartQueryTime(startQueryTime);
        orderStatisticsQueryDto.setEndQueryTime(endQueryTime);
        orderStatisticsQueryDto.setAutCode(autCode);
        orderStatisticsQueryDto.setSn(sn);
        return statisticsService.queryOrderStatistics(orderStatisticsQueryDto);
    }
}
